Skip to content

level: don't read a key name as a line's level - #38

Merged
mayankpande88 merged 3 commits into
mainfrom
fix/json-level-guess-from-key-names
Nov 27, 2026
Merged

mayankpande88 merged 3 commits into
mainfrom
fix/json-level-guess-from-key-names

Conversation

@reckless-sherixx

@reckless-sherixx reckless-sherixx commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A line with no level key but an error key was emitted as an error. Go access logs (Echo, Gin) are the common case: every {"method":"GET","uri":"/healthz","status":200,"error":""} health check came out as error, and so did logfmt ... status=200 error="".

The cause was GuessLevel, which read the key "error" as the word error.

  • GuessLevel now skips a key name: a quoted word directly followed by : ("error":), or a key=value token (error=""). Values still count, so {"type":"error"}, {"msg":"error: db down"} and ERROR[req=abc] ... keep their level. The change is in GuessLevel, so it covers single-line JSON, logfmt and multiline JSON alike.
  • Both parseJson modes now read the same level keys. structuredFromJSON reads the keys jsonLogFromFields reads (@l, severity_text, numeric pino levels) before the older patternLevelKeys, so pino {"level":50} and Serilog {"@l":"Error"} are error with parseJson off too. A numeric level is accepted however it is written (50, 50.0, 5e1).

Trade-off: in plain text, a quoted word followed by a colon ("ERROR": none) now reads as a key, not a level. A dotted logfmt key (error.code=5) is still read as error, as on main.

No API change. Each line is decoded once, as on main.

How to verify

go test -count=1 -run 'TestParserLevelNotFromKeyNames|TestGuessLevelSkipsKeys' ./.... TestParserLevelNotFromKeyNames runs 16 lines through NewParser under both parseJson modes. They include the two health checks, logfmt and multiline error keys, nested and non-level-key levels, and pino, 50.0, @l and severity_text levels. On main, the error-key cases fail, and so do the numeric, @l and severity_text cases with parseJson off.

What was run
  • go vet ./..., go build -mod=readonly ./..., go test -count=1 ./... pass. -race was not run locally (no cgo on the dev box); CI runs it.
  • The old and new GuessLevel were compared on about 45 plain-text lines (glog/klog, nginx, apache, MySQL, Python logging, logback, docker level=, UTF-8). They differ only on the intended key skips.

A container log line has no level from its source, so the line collector
guesses one from the raw text. For a JSON line that has no level key, that
guess came from the key names: an access log's {"status":200,"error":""}
read as an error, because "error" is a key. JSON parsing kept the guess,
since it found no level key to override it with.

The collector now records that a level was guessed. When the line turns
out to be a JSON object without a level key, the parser guesses again from
the JSON message field alone ({"msg":"error: db down"} is still an error),
or leaves the level unknown. A level the source reports, such as a journald
priority, is kept. Both parseJson modes behave the same.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a levelGuessed flag to track whether a log level was guessed from the content. In parser.go, this flag is used to refine the log level for JSON lines that lack an explicit level key, preventing false-positive level guesses from key names (such as "error"). A performance optimization was suggested to avoid the expensive JSON decoding in parser.go when msg.Level is already LevelUnknown.

Comment thread parser.go Resolved

@mayankpande88 mayankpande88 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran sample lines through NewParser on this head. The fix works for the access-log case, but it turns some lines that used to be error into unknown, and the two parseJson modes still give different levels. Details are inline.

One more point, outside the diff (level.go, GuessLevel): the root cause is that GuessLevel reads a key name ("error":, error=) as a level word, and this PR patches only single-line JSON in Parser.inc. These lines are still error in both modes:

  • logfmt: method=GET uri=/healthz status=200 error="", because ParseStructuredLog returns GuessLevel(line) as its level
  • multiline JSON: {"method":"GET","status":200,"error":""} followed by a continuation line

If GuessLevel skipped a token followed by ": or = (a key, not a value), every format would be covered, and the re-guess block here might not be needed.

Comment thread parser.go Resolved
Comment thread parser.go Resolved
Comment thread parser.go Resolved
Comment thread parser.go Resolved
…e key set

GuessLevel now skips a key, a quoted word followed by a colon ("error":)
or a word=value pair (error=""), instead of reading it as a level word.
That fixes every format, not just single-line JSON: logfmt and multiline
JSON lines with an empty error field are no longer errors.

This replaces the re-guess from the JSON message field and the
levelGuessed flag, which turned lines whose level sat outside a level
key into unknown and decoded the line a second time.

structuredFromJSON reads the level from the keys jsonLogFromFields
reads (@l, severity_text, numeric levels) before patternLevelKeys, so
a line gets the same level with and without parseJson. A numeric level
is accepted however it is written (50, 50.0, 5e1).
@reckless-sherixx

Copy link
Copy Markdown
Contributor Author

@mayankpande88 Thanks, I took the GuessLevel route you suggested, in f72585f. A quoted word directly followed by : ("error":) or a key=value token (error="") is no longer read as a level word. That covers logfmt and multiline JSON as well, and the re-guess block is gone.

I checked plain-text formats that put brackets after the level word or quote a phrase that ends in one: ERROR[req=abc] ..., WARN(user=bob) ... and Failed "connection error": timeout all keep their level, and there are unit tests for each in TestGuessLevelSkipsKeys. Two things are left as they are. A quoted word followed by a colon in plain text ("ERROR": none) now reads as a key. A dotted logfmt key (error.code=5) is still read as error, the same as on main.

@reckless-sherixx reckless-sherixx changed the title parser: don't read a JSON line's key names as its level level: don't read a key name as a line's level Nov 26, 2026
@mayankpande88
mayankpande88 merged commit 1f96adc into main Nov 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants